<template>
  <div class="chat-input">
    <div class="input-container">
      <textarea
        ref="inputRef"
        v-model="inputText"
        class="input-field"
        placeholder="输入消息..."
        @keydown="handleKeydown"
        @input="adjustHeight"
        :disabled="isLoading"
        rows="1"
      ></textarea>
      
      <!-- 发送/停止按钮 -->
      <button
        class="action-button"
        @click="handleAction"
        :disabled="!canSend && !isLoading"
        :title="isLoading ? '停止生成' : '发送消息'"
        :class="{ 'stop-button': isLoading, 'send-button': !isLoading }"
      >
        <!-- 发送图标 -->
        <svg v-if="!isLoading" width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
          <path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/>
        </svg>
        
        <!-- 停止图标 -->
        <svg v-else width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
          <path d="M6 6h12v12H6z"/>
        </svg>
      </button>
    </div>
    
    <!-- 输入提示 -->
    <div class="input-hints" v-if="!isLoading">
      <span class="hint-text">按 Enter 发送，Shift + Enter 换行</span>
      <span class="char-count" v-if="inputText.length > 0">{{ inputText.length }}/2000</span>
    </div>
    
    <!-- 流式状态提示 -->
    <div class="streaming-status" v-if="isLoading">
      <div class="status-indicator">
        <div class="pulse-dot"></div>
        <span>AI 正在回复中...</span>
      </div>
      <span class="stop-hint">点击停止按钮可中断生成</span>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, nextTick, watch } from 'vue'

const props = defineProps({
  isLoading: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['send', 'stop'])

const inputRef = ref(null)
const inputText = ref('')

// 是否可以发送
const canSend = computed(() => {
  return inputText.value.trim() && inputText.value.length <= 2000 && !props.isLoading
})

// 处理主要操作（发送或停止）
const handleAction = () => {
  if (props.isLoading) {
    // 正在加载时，点击停止
    emit('stop')
  } else if (canSend.value) {
    // 可以发送时，发送消息
    handleSend()
  }
}

// 处理发送
const handleSend = () => {
  if (!canSend.value) return
  
  const message = inputText.value.trim()
  inputText.value = ''
  adjustHeight()
  
  emit('send', message)
}

// 处理键盘事件
const handleKeydown = (event) => {
  if (event.key === 'Enter' && !event.shiftKey) {
    event.preventDefault()
    if (props.isLoading) {
      // 如果正在加载，Enter 键停止生成
      emit('stop')
    } else {
      // 否则发送消息
      handleSend()
    }
  } else if (event.key === 'Escape' && props.isLoading) {
    // ESC 键也可以停止生成
    event.preventDefault()
    emit('stop')
  }
}

// 自动调整高度
const adjustHeight = async () => {
  await nextTick()
  const textarea = inputRef.value
  if (textarea) {
    textarea.style.height = 'auto'
    textarea.style.height = Math.min(textarea.scrollHeight, 120) + 'px'
  }
}

// 聚焦输入框
const focus = () => {
  inputRef.value?.focus()
}

// 清空输入
const clear = () => {
  inputText.value = ''
  adjustHeight()
}

// 监听加载状态变化，自动聚焦
watch(() => props.isLoading, (newLoading, oldLoading) => {
  if (oldLoading && !newLoading) {
    // 从加载状态变为非加载状态，自动聚焦
    nextTick(() => {
      focus()
    })
  }
})

defineExpose({
  focus,
  clear
})
</script>

<style scoped>
.chat-input {
  background: white;
  border-top: 1px solid #e9ecef;
  padding: 16px 20px;
}

.input-container {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  background: #f8f9fa;
  border: 2px solid #e9ecef;
  border-radius: 24px;
  padding: 8px 12px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-container:focus-within {
  border-color: #007bff;
  box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.1);
}

.input-field {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  resize: none;
  font-size: 14px;
  line-height: 1.5;
  padding: 8px 0;
  min-height: 20px;
  max-height: 120px;
  font-family: inherit;
}

.input-field::placeholder {
  color: #6c757d;
}

.input-field:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.action-button {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}

.send-button {
  background: #007bff;
  color: white;
}

.send-button:hover:not(:disabled) {
  background: #0056b3;
  transform: scale(1.05);
}

.send-button:disabled {
  background: #6c757d;
  cursor: not-allowed;
  opacity: 0.6;
}

.stop-button {
  background: #dc3545;
  color: white;
  animation: pulse-red 2s infinite;
}

.stop-button:hover {
  background: #c82333;
  transform: scale(1.05);
}

@keyframes pulse-red {
  0% {
    box-shadow: 0 0 0 0 rgba(220, 53, 69, 0.4);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(220, 53, 69, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(220, 53, 69, 0);
  }
}

.input-hints {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  font-size: 12px;
  color: #6c757d;
}

.hint-text {
  opacity: 0.8;
}

.char-count {
  font-weight: 500;
  color: #495057;
}

.char-count.over-limit {
  color: #dc3545;
}

.streaming-status {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  font-size: 12px;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #007bff;
  font-weight: 500;
}

.pulse-dot {
  width: 8px;
  height: 8px;
  background: #007bff;
  border-radius: 50%;
  animation: pulse-blue 1.5s infinite;
}

@keyframes pulse-blue {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.5;
    transform: scale(1.2);
  }
}

.stop-hint {
  color: #6c757d;
  opacity: 0.8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .chat-input {
    padding: 12px 16px;
  }
  
  .input-container {
    gap: 8px;
    padding: 6px 10px;
  }
  
  .action-button {
    width: 36px;
    height: 36px;
  }
  
  .input-hints,
  .streaming-status {
    font-size: 11px;
    margin-top: 6px;
  }
  
  .streaming-status {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}

/* 字符数限制样式 */
.input-field.over-limit {
  color: #dc3545;
}

.char-count.over-limit {
  color: #dc3545;
  font-weight: 600;
}
</style>